package com.yj.forum.controller;

import com.yj.forum.common.AppConfig;
import com.yj.forum.common.AppResult;
import com.yj.forum.common.ResultCode;
import com.yj.forum.exception.ApplicationException;
import com.yj.forum.model.Article;
import com.yj.forum.model.ArticleReply;
import com.yj.forum.model.User;
import com.yj.forum.services.IArticleReplyService;
import com.yj.forum.services.IArticleService;
import com.yj.forum.utils.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Api(tags = "回复接⼝")
@Slf4j
@RequestMapping("/reply")
@RestController
public class ArticleReplyController {
    @Resource
    private IArticleService articleService;
    @Resource
    private IArticleReplyService articleReplyService;
    /**
     * 回复帖⼦
     * @param articleId 帖⼦Id
     * @param content 回复内容
     * @return AppResult
     */
    @ApiOperation("回复帖⼦")
    @PostMapping("/create")
    public AppResult create(HttpServletRequest request,
                            @ApiParam("帖⼦Id")@RequestParam("articleId") Long articleId,
                            @ApiParam("回复内容")@RequestParam("content") String content
                            ) {
        //非空校验
        if(articleId == null || articleId <= 0 || StringUtil.isEmpty(content)) {
            return AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE);
        }
        //获取用户信息
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        //用户是否禁言
        if (user.getState() != 0) {
            log.warn(ResultCode.FAILED_USER_BANNED.toString() + ", userId = " + user.getId());
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        //获取帖子信息
        Article article = articleService.selectById(articleId);
        // 校验帖⼦信息
        if (article == null) {
            log.warn("修改的帖⼦不存在. articleId = " + articleId);
            return AppResult.failed(ResultCode.FAILED_ARTICLE_BANNED);
        }
        if (article.getState() != 0 || article.getDeleteState() != 0) {
            log.warn("帖⼦状态异常. articleId = " + articleId + ", state = " +
                    article.getState() + ", delete state = " + article.getDeleteState());
            return AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE);
        }
        // 构造数据
        ArticleReply articleReply = new ArticleReply();
        articleReply.setArticleId(articleId);
        articleReply.setPostUserId(user.getId());
        articleReply.setContent(content);
        // 调⽤dao
        articleReplyService.create(articleReply);
        // 返回结果
        return AppResult.success();
    }
    /**
     * 回复帖⼦
     * @param articleId 帖⼦Id
     * @return AppResult
     */
    @ApiOperation("回复帖⼦列表")
    @GetMapping("/getReplies")
    public AppResult<List<ArticleReply>> getRepliesByArticleId(@ApiParam("帖⼦Id") @RequestParam("articleId") Long articleId) {
        //非空校验
        if(articleId == null || articleId <= 0) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString()+" 该文章不存在 articleId: "+articleId);
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        // 查询帖⼦是否存在
        Article article = articleService.selectById(articleId);
        if(article == null || article.getState() != 0 || article.getDeleteState() != 0) {
            log.warn(ResultCode.FAILED_ARTICLE_IS_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_ARTICLE_IS_NOT_EXISTS));
        }
        //查询结果
        List<ArticleReply> articleReplies = articleReplyService.selectByArticleId(articleId);
        return AppResult.success(articleReplies);
    }
}
